<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 登陆表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFromRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 标题 -->
        <p style="text-align:center;font-size:28px;color:#dcdcdc">Login</p>
        <!-- 用户名 -->
        <el-form-item prop="username" label>
          <el-input v-model="loginForm.username" placeholder="请输入用户名">
            <i slot="prefix" class="el-input__icon iconfont icon-user"></i>
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码">
            <i slot="prefix" class="el-input__icon iconfont icon-password"></i>
          </el-input>
        </el-form-item>
        <!-- 登录重置按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
const qs = require("qs");
import axios from "axios";
export default {
  data() {
    return {
      // 登录组件的表单数据绑定
      loginForm: {
        username: "",
        password: ""
      },
      // 登录组件表单验证
      loginFromRules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur"
          }
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 5 到 10 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    // 实现登录组件的重置
    resetLoginForm() {
      // console.log(this)
      this.$refs.loginFormRef.resetFields();
    },
    // 实现表单预验证
    login() {
      axios.defaults.headers.post["Content-Type"] =
        "application/x-www-form-urlencoded";
      axios({
        method: "post",
        url: "http://120.76.142.108:8080/uaa/auth/form",
        data: qs.stringify(this.loginForm),
        headers: {
          "Content-Type": "application/x-www-form-urlencoded;"
        }
      }).then(data => {
        console.log(data);
        if (data.data.code === 0) {
          this.$message({
            type: "success",
            message: data.data.msg
          });
          sessionStorage.setItem("userInfo", JSON.stringify(data.data.data));
          this.$router.push("/home");
        } else {
          this.$message({
            type: "error",
            message: data.data.msg
          });
        }
      });
      // this.$refs.loginFormRef.validate(async valid => {
      //   if (!valid) return
      //   const { data: res } = await this.$http.post('/uaa/auth/form', qs.stringify(this.loginForm))
      //   console.log(res)
      //   if (res.code !== 0) {
      //     // 提示框
      //     return this.$message.error('登录失败，账号或密码输入错误！')
      //   } else {
      //     this.$message.success('登录成功')
      //   }
      //   // 使用sessionStorage来实现登录成功后的行为
      //   // window.sessionStorage.setItem('access_token', res.data.access_token)
      //   sessionStorage.setItem("userInfo", JSON.stringify(data.data.data));
      //   this.$router.push('/home')
      // })
    }
  },
  components: {}
};
</script>

<style lang="scss" scoped>
.login_container {
  // background-color: #2b4b6b;
  height: 100vh;
  position: relative;
  background-image: url("../assets/back-book.jpg");
  background-size: 100% 100%;
  height: 100%;
  position: fixed;
  width: 100%;
  //   display: flex;
  //   justify-content: center;
  //   align-items: center;
  .login_box {
    width: 340px;
    height: 280px;
    background-color: white;
    border-radius: 3px;
    position: absolute;
    left: 70%;
    top: 20%;
    opacity: 0.7;
    .btns {
      display: flex;
      justify-content: flex-end;
    }
    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 10px;
      box-sizing: border-box;
    }
  }
}
.el-input /deep/.el-input_inner{
  width: 320px;
  height: 50px;
}
i{
  color: #C39862;
}
.el-input {
        /deep/ .el-input__inner {
          border-top: none;
          border-left: none;
          border-right: none;
          border-radius: 0px;
          // width: 235px;
          height: 40px;
          &::placeholder{
            font-size: 12px;
            transform: translateY(3px);
          }
          &:focus{
color:#C39862;
          }

        }
      }
</style>
